package cn.dengta.webapp.user.entity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.X;

/**
 * 聊天数据统计
 */
@Getter
@Setter
@Accessors(chain = true)
public class GreetStatistics extends TimeBasedEntity {

	/** 用户ID */
	private Long userId;
	/** 总在线时长(秒) */
	private Long onlineTime;
	/** 用户在线聊天人数 */
	private Integer onlineCount;
	/** 总聊天场次 */
	private Integer chatCount;
	/** 聊天人数 */
	public Integer chatUserCount;
	/** 聊天消耗总钻石数 */
	public Long chatCostCount;
	/** 女生总搭讪次數 */
	private Integer femaleGreetTotalCount;
	/** 搭讪对方回复数 */
	private Integer maleAnswerCount;
	/** 女生收到搭讪次數 */
	private Integer femaleGreetCount;
	/** 搭讪自己回复数 */
	private Integer femaleAnswerCount;
	/** 搭讪总数 */
	private Integer greetCount;
	/** 搭讪价值 */
	private BigDecimal greetPrice;
	/** 文字聊天总数 */
	private Integer msgCount;
	/** 文字聊天用户数 */
	private Integer msgUserCount;
	/** 文字价值 */
	private BigDecimal msgPrice;
	/** 视频通话时长 */
	private Long callLength;
	/** 语音通话时长 */
	private Long audioLength;
	/** 视频收益 */
	private BigDecimal videoIncome;
	/** 语音收益 */
	private BigDecimal audioIncome;
	/** 礼物总收益 */
	private BigDecimal giftIncome;
	/** 总收益 */
	private BigDecimal totalIncome;
	/** 统计日期 */
	private Date reportTime;
	/** 一级推广员 */
	private Long agentId;
	/** 二级推广员 */
	private Long agent2Id;

	public GreetStatistics init(GreetStatistics input, Long userId, Date reportTime, Date now) {
		this.userId = userId;
		this.onlineTime = X.expectNotNull(input.onlineTime, 0L);
		this.chatUserCount = X.expectNotNull(input.chatUserCount, 0);
		this.chatCount = X.expectNotNull(input.chatCount, 0);
		this.chatCostCount = X.expectNotNull(input.chatCostCount, 0L);
		this.femaleGreetTotalCount = X.expectNotNull(input.femaleGreetTotalCount, 0);
		this.maleAnswerCount = X.expectNotNull(input.maleAnswerCount, 0);
		this.femaleGreetCount = X.expectNotNull(input.femaleGreetCount, 0);
		this.femaleAnswerCount = X.expectNotNull(input.femaleAnswerCount, 0);
		this.greetCount = X.expectNotNull(input.greetCount, 0);
		this.greetPrice = X.expectNotNull(input.greetPrice, BigDecimal.ZERO);
		this.msgCount = X.expectNotNull(input.msgCount, 0);
		this.msgUserCount = X.expectNotNull(input.msgUserCount, 0);
		this.msgPrice = X.expectNotNull(input.msgPrice, BigDecimal.ZERO);
		this.callLength = X.expectNotNull(input.callLength, 0L);
		this.videoIncome = X.expectNotNull(input.videoIncome, BigDecimal.ZERO);
		this.giftIncome = X.expectNotNull(input.giftIncome, BigDecimal.ZERO);
		this.totalIncome = X.expectNotNull(input.totalIncome, BigDecimal.ZERO);
		this.reportTime = reportTime;
		super.initTime(now);
		return this;
	}

	@JSONField(serialize = false)
	public String getOnlineTime_() {
		if (onlineTime != null) {
			float secondsToHour = 3600F;
			return onlineTime <= 60 ? onlineTime + "秒" : (onlineTime >= secondsToHour ? String.format("%.2f", onlineTime / secondsToHour) + "小时" : TimeUnit.SECONDS.toMinutes(onlineTime) + "分");
		}
		return null;
	}

	@JSONField(serialize = false)
	public String getCallLength_() {
		if (callLength != null) {
			float secondsToHour = 3600F;
			return callLength <= 60 ? callLength + "秒" : (callLength >= secondsToHour ? String.format("%.2f", callLength / secondsToHour) + " 小时" : TimeUnit.SECONDS.toMinutes(callLength) + " 分");
		}
		return null;
	}

	@JSONField(serialize = false)
	public long getMaleAnswerRate() {
		return femaleGreetTotalCount > 0 ? (maleAnswerCount * 100L) / femaleGreetTotalCount : 0L;
	}

	@JSONField(serialize = false)
	public long getFemaleAnswerRate() {
		return femaleGreetCount > 0 ? (femaleAnswerCount * 100L) / femaleGreetCount : 0;
	}

	@Override
	public Long getId() {
		return id;
	}

	public static final String USER_ID = "user_id";
	public static final String VIDEO_CHAT_COUNT = "video_chat_count";
	public static final String ONLINE_COUNT = "online_count";
	public static final String ONLINE_TIME = "online_time";
	public static final String CHAT_COUNT = "chat_count";
	public static final String FEMALE_GREET_TOTAL_COUNT = "female_greet_total_count";
	public static final String MALE_ANSWER_COUNT = "male_answer_count";
	public static final String MALE_ANSWER_RATE = "male_answer_rate";
	public static final String FEMALE_GREET_COUNT = "female_greet_count";
	public static final String FEMALE_ANSWER_COUNT = "female_answer_count";
	public static final String FEMALE_ANSWER_RATE = "female_answer_rate";
	public static final String GREET_COUNT = "greet_count";
	public static final String GREET_PRICE = "greet_price";
	public static final String MSG_COUNT = "msg_count";
	public static final String MSG_PRICE = "msg_price";
	public static final String CALL_LENGTH = "call_length";
	public static final String VIDEO_INCOME = "video_income";
	public static final String GIFT_INCOME = "gift_income";
	public static final String REPORT_TIME = "report_time";
	public static final String AGENT_ID = "agent_id";
	public static final String AGENT2_ID = "agent2_id";

}
